﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	public class RadiatorConnHPipesHelper
	{
		public RadiatorConnHPipesHelper(List<Pipe> riserPipe)
		{
			this.m_pipeList = riserPipe;
		}

		public List<Pipe> ConnPipesByOverBridge(Pipe side, Pipe main, Pipe beOverPipe, XYZ overVector, ref string connMSG)
		{
			if (overVector == null)
			{
				overVector = new XYZ(0.0, 0.0, -1.0);
			}
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(side);
			YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(main);
			YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(beOverPipe);
			List<Pipe> list = new List<Pipe>();
			Document document = side.Document;
			double diameter = hypiping.GetDiameter();
			double diameter2 = hypiping2.GetDiameter();
			YJKLine hyline = new YJKLine(hypiping2.StartConnector.Origin, hypiping2.EndConnector.Origin);
			hyline.MakeUnBound();
			YJKLine hyline2 = new YJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
			XYZ closePipePoint = this.GetClosePipePoint(hypiping, hypiping2);
			hypiping.GetCloseConnector(closePipePoint, false);
			XYZ project = hyline.GetProject(closePipePoint);
			XYZ xyz = new XYZ(project.X, project.Y, closePipePoint.Z);
			double num = project.DistanceTo(xyz);
			IList<Pipe> list2 = new List<Pipe>();
			YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
			double z = hypiping.StartConnector.Origin.Z;
			double z2 = hypiping3.StartConnector.Origin.Z;
			bool flag = Math.Abs(z - z2) <= 0.0001;
			HYPipeConnect hypipeConnect = new HYPipeConnect();
			List<Pipe> collection = new List<Pipe>();
			if (num <= 0.016404199475065617)
			{
				Pipe pipe = hypiping.Piping;
				if (!hyline2.IsPointOnCurveLine(project))
				{
					Pipe pipe2 = YJKRevitTools.NewPipe(hypiping.Piping, closePipePoint, project);
					this.SetDiameter(pipe, diameter);
					list2 = new List<Pipe>();
					list2.Add(pipe);
					list2.Add(pipe2);
					hypipeConnect.NewElbowFitting(list2, true);
					pipe = pipe2;
				}
				if (hyline.IsPointOnLine(project) && hyline.StartPoint.DistanceTo(project) >= diameter2 * 1.5 && hyline.EndPoint.DistanceTo(project) >= diameter2 * 1.5)
				{
					List<Pipe> list3 = hypiping2.Separate(project);
					hyrotateFamilyInstance.MovePiping(list3[0]);
					hyrotateFamilyInstance.MovePiping(list3[1]);
					hyrotateFamilyInstance.MovePiping(pipe);
					document.Regenerate();
					list2 = new List<Pipe>();
					list2.Add(list3[0]);
					list2.Add(list3[1]);
					list2.Add(pipe);
					hypipeConnect.NewTeeFitting(ref list2, true);
					list.Add(list3[0]);
					list.Add(list3[1]);
					Pipe separatePipe = pipe;
					hypipeConnect.NewUnionFittings(separatePipe, beOverPipe, document, overVector, ref connMSG, ref collection);
				}
				else
				{
					list2 = new List<Pipe>();
					list2.Add(hypiping2.Piping);
					list2.Add(pipe);
					hypipeConnect.NewElbowFitting(list2, true);
					list.Add(hypiping2.Piping);
					Pipe separatePipe = pipe;
					hypipeConnect.NewUnionFittings(separatePipe, beOverPipe, document, overVector, ref connMSG, ref collection);
				}
			}
			else if (num > (diameter + diameter2) * 1.5)
			{
				Pipe pipe3 = hypiping.Piping;
				if (!hyline2.IsPointOnCurveLine(xyz))
				{
					Pipe pipe4 = YJKRevitTools.NewPipe(hypiping.Piping, closePipePoint, xyz);
					this.SetDiameter(pipe3, diameter);
					list2 = new List<Pipe>();
					list2.Add(pipe3);
					list2.Add(pipe4);
					hypipeConnect.NewElbowFitting(list2, true);
					pipe3 = pipe4;
				}
				Pipe pipe5 = YJKRevitTools.NewPipe(hypiping.Piping, xyz, project);
				this.SetDiameter(pipe5, diameter);
				hyrotateFamilyInstance.MovePiping(pipe5);
				list2 = new List<Pipe>();
				list2.Add(pipe3);
				list2.Add(pipe5);
				hypipeConnect.NewElbowFitting(list2, true);
				if (hyline.IsPointOnLine(project) && hyline.StartPoint.DistanceTo(project) >= diameter2 * 1.5 && hyline.EndPoint.DistanceTo(project) >= diameter2 * 1.5)
				{
					List<Pipe> list3 = hypiping2.Separate(project);
					hyrotateFamilyInstance.MovePiping(list3[0]);
					hyrotateFamilyInstance.MovePiping(list3[1]);
					hyrotateFamilyInstance.MovePiping(pipe5);
					document.Regenerate();
					list2 = new List<Pipe>();
					list2.Add(list3[0]);
					list2.Add(list3[1]);
					list2.Add(pipe5);
					hypipeConnect.NewTeeFitting(ref list2, true);
					list.Add(list3[0]);
					list.Add(list3[1]);
				}
				else
				{
					list2 = new List<Pipe>();
					list2.Add(hypiping2.Piping);
					list2.Add(pipe5);
					hypipeConnect.NewElbowFitting(list2, true);
					list.Add(hypiping2.Piping);
				}
				Pipe separatePipe;
				if (flag)
				{
					separatePipe = pipe3;
				}
				else
				{
					separatePipe = pipe5;
				}
				hypipeConnect.NewUnionFittings(separatePipe, beOverPipe, document, overVector, ref connMSG, ref collection);
				if (flag)
				{
					this.m_pipeList.Add(pipe5);
				}
				else
				{
					this.m_pipeList.AddRange(collection);
				}
			}
			else
			{
				connMSG = "两管间距不合适，不能进行连接";
			}
			return list;
		}

		public List<Pipe> ConnPipesByDoubleElbow(Pipe side, Pipe main, Pipe beOverPipe, ref string connMSG)
		{
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(side);
			YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(main);
			YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(beOverPipe);
			List<Pipe> list = new List<Pipe>();
			Document document = side.Document;
			double diameter = hypiping.GetDiameter();
			double diameter2 = hypiping2.GetDiameter();
			double diameter3 = hypiping3.GetDiameter();
			YJKLine hyline = new YJKLine(hypiping2.StartConnector.Origin, hypiping2.EndConnector.Origin);
			hyline.MakeUnBound();
			YJKLine hyline2 = new YJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
			XYZ closePipePoint = this.GetClosePipePoint(hypiping, hypiping2);
			XYZ project = hyline.GetProject(closePipePoint);
			XYZ xyz = new XYZ(project.X, project.Y, closePipePoint.Z);
			double num = project.DistanceTo(xyz);
			IList<Pipe> list2 = new List<Pipe>();
			YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
			double minDoubleElbowLength = RadiatorConnHelper.GetMinDoubleElbowLength(diameter3 / 2.0, diameter2 / 2.0, diameter / 2.0);
			HYPipeConnect hypipeConnect = new HYPipeConnect();
			new List<Pipe>();
			if (num <= 0.016404199475065617)
			{
				Pipe pipe = hypiping.Piping;
				if (!hyline2.IsPointOnCurveLine(project))
				{
					Pipe pipe2 = YJKRevitTools.NewPipe(hypiping.Piping, closePipePoint, project);
					this.SetDiameter(pipe, diameter);
					list2 = new List<Pipe>();
					list2.Add(pipe);
					list2.Add(pipe2);
					hypipeConnect.NewElbowFitting(list2, true);
					pipe = pipe2;
				}
				if (hyline.IsPointOnLine(project) && hyline.StartPoint.DistanceTo(project) >= diameter2 * 1.5 && hyline.EndPoint.DistanceTo(project) >= diameter2 * 1.5)
				{
					List<Pipe> list3 = hypiping2.Separate(project);
					hyrotateFamilyInstance.MovePiping(list3[0]);
					hyrotateFamilyInstance.MovePiping(list3[1]);
					hyrotateFamilyInstance.MovePiping(pipe);
					document.Regenerate();
					list2 = new List<Pipe>();
					list2.Add(list3[0]);
					list2.Add(list3[1]);
					list2.Add(pipe);
					hypipeConnect.NewTeeFitting(ref list2, true);
					list.Add(list3[0]);
					list.Add(list3[1]);
				}
				else
				{
					list2 = new List<Pipe>();
					list2.Add(hypiping2.Piping);
					list2.Add(pipe);
					hypipeConnect.NewElbowFitting(list2, true);
					list.Add(hypiping2.Piping);
				}
			}
			else if (num > minDoubleElbowLength)
			{
				Pipe pipe3 = hypiping.Piping;
				if (!hyline2.IsPointOnCurveLine(xyz))
				{
					Pipe pipe4 = YJKRevitTools.NewPipe(hypiping.Piping, closePipePoint, xyz);
					this.SetDiameter(pipe3, diameter);
					list2 = new List<Pipe>();
					list2.Add(pipe3);
					list2.Add(pipe4);
					hypipeConnect.NewElbowFitting(list2, true);
					pipe3 = pipe4;
				}
				Pipe pipe5 = YJKRevitTools.NewPipe(hypiping.Piping, xyz, project);
				this.SetDiameter(pipe5, diameter);
				hyrotateFamilyInstance.MovePiping(pipe5);
				list2 = new List<Pipe>();
				list2.Add(pipe3);
				list2.Add(pipe5);
				hypipeConnect.NewElbowConnect(pipe3, pipe5, beOverPipe, document, ref connMSG);
				if (hyline.IsPointOnLine(project) && hyline.StartPoint.DistanceTo(project) >= diameter2 * 1.5 && hyline.EndPoint.DistanceTo(project) >= diameter2 * 1.5)
				{
					List<Pipe> list3 = hypiping2.Separate(project);
					hyrotateFamilyInstance.MovePiping(list3[0]);
					hyrotateFamilyInstance.MovePiping(list3[1]);
					hyrotateFamilyInstance.MovePiping(pipe5);
					document.Regenerate();
					list2 = new List<Pipe>();
					list2.Add(list3[0]);
					list2.Add(list3[1]);
					list2.Add(pipe5);
					hypipeConnect.NewTeeFitting(ref list2, true);
					list.Add(list3[0]);
					list.Add(list3[1]);
				}
				else
				{
					list2 = new List<Pipe>();
					list2.Add(hypiping2.Piping);
					list2.Add(pipe5);
					hypipeConnect.NewElbowFitting(list2, true);
					list.Add(hypiping2.Piping);
				}
				this.m_pipeList.Add(pipe5);
			}
			else
			{
				connMSG = "两管间距不合适，不能进行连接";
			}
			return list;
		}

		public List<Pipe> ConnPipesBySingleElbow(Pipe side, Pipe toConnPipe, Pipe toOverPipe, ref string connMSG)
		{
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(side);
			YJKPiping<Pipe> hypiping2 = new YJKPiping<Pipe>(toConnPipe);
			YJKPiping<Pipe> hypiping3 = new YJKPiping<Pipe>(toOverPipe);
			List<Pipe> list = new List<Pipe>();
			Document document = side.Document;
			double diameter = hypiping.GetDiameter();
			double diameter2 = hypiping2.GetDiameter();
			double diameter3 = hypiping3.GetDiameter();
			YJKLine hyline = new YJKLine(hypiping2.StartConnector.Origin, hypiping2.EndConnector.Origin);
			hyline.MakeUnBound();
			YJKLine hyline2 = new YJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
			XYZ closePipePoint = this.GetClosePipePoint(hypiping, hypiping2);
			XYZ project = hyline.GetProject(closePipePoint);
			XYZ xyz = new XYZ(project.X, project.Y, closePipePoint.Z);
			double num = project.DistanceTo(xyz);
			IList<Pipe> list2 = new List<Pipe>();
			YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
			double minSingleElbowLength = RadiatorConnHelper.GetMinSingleElbowLength(diameter3 / 2.0, diameter / 2.0);
			HYPipeConnect hypipeConnect = new HYPipeConnect();
			new List<Pipe>();
			if (num <= 0.016404199475065617)
			{
				Pipe pipe = hypiping.Piping;
				if (!hyline2.IsPointOnCurveLine(project))
				{
					Pipe pipe2 = YJKRevitTools.NewPipe(hypiping.Piping, closePipePoint, project);
					this.SetDiameter(pipe, diameter);
					list2 = new List<Pipe>();
					list2.Add(pipe);
					list2.Add(pipe2);
					hypipeConnect.NewElbowFitting(list2, true);
					pipe = pipe2;
				}
				if (hyline.IsPointOnLine(project) && hyline.StartPoint.DistanceTo(project) >= diameter2 * 1.5 && hyline.EndPoint.DistanceTo(project) >= diameter2 * 1.5)
				{
					List<Pipe> list3 = hypiping2.Separate(project);
					hyrotateFamilyInstance.MovePiping(list3[0]);
					hyrotateFamilyInstance.MovePiping(list3[1]);
					hyrotateFamilyInstance.MovePiping(pipe);
					document.Regenerate();
					list2 = new List<Pipe>();
					list2.Add(list3[0]);
					list2.Add(list3[1]);
					list2.Add(pipe);
					hypipeConnect.NewTeeFitting(ref list2, true);
					list.Add(list3[0]);
					list.Add(list3[1]);
				}
				else
				{
					list2 = new List<Pipe>();
					list2.Add(hypiping2.Piping);
					list2.Add(pipe);
					hypipeConnect.NewElbowFitting(list2, true);
					list.Add(hypiping2.Piping);
				}
			}
			else if (num > minSingleElbowLength)
			{
				Pipe pipe3 = hypiping.Piping;
				if (!hyline2.IsPointOnCurveLine(xyz))
				{
					Pipe pipe4 = YJKRevitTools.NewPipe(hypiping.Piping, closePipePoint, xyz);
					this.SetDiameter(pipe3, diameter);
					list2 = new List<Pipe>();
					list2.Add(pipe3);
					list2.Add(pipe4);
					hypipeConnect.NewElbowFitting(list2, true);
					pipe3 = pipe4;
				}
				XYZ xyz2 = closePipePoint.Subtract(xyz).Normalize();
				XYZ startXYZ = xyz.Add(xyz2 * num);
				Pipe pipe5 = YJKRevitTools.NewPipe(hypiping.Piping, startXYZ, project);
				this.SetDiameter(pipe5, diameter);
				hyrotateFamilyInstance.MovePiping(pipe5);
				list2 = new List<Pipe>();
				list2.Add(pipe3);
				list2.Add(pipe5);
				hypipeConnect.NewElbowFitting(list2, true);
				if (hyline.IsPointOnLine(project) && hyline.StartPoint.DistanceTo(project) >= diameter2 * 1.5 && hyline.EndPoint.DistanceTo(project) >= diameter2 * 1.5)
				{
					List<Pipe> list3 = hypiping2.Separate(project);
					hyrotateFamilyInstance.MovePiping(list3[0]);
					hyrotateFamilyInstance.MovePiping(list3[1]);
					hyrotateFamilyInstance.MovePiping(pipe5);
					document.Regenerate();
					list2 = new List<Pipe>();
					list2.Add(list3[0]);
					list2.Add(list3[1]);
					list2.Add(pipe5);
					hypipeConnect.NewTeeFitting(ref list2, true);
					list.Add(list3[0]);
					list.Add(list3[1]);
				}
				else
				{
					list2 = new List<Pipe>();
					list2.Add(hypiping2.Piping);
					list2.Add(pipe5);
					hypipeConnect.NewElbowFitting(list2, true);
					list.Add(hypiping2.Piping);
				}
			}
			else
			{
				connMSG = "两管间距不合适，不能进行连接";
			}
			return list;
		}

		private void SetDiameter(Pipe branchPipe, double diameter)
		{
			new YJKPiping<Pipe>(branchPipe).SetDiameter(diameter * 304.8);
		}

		private XYZ GetClosePipePoint(YJKPiping<Pipe> hySide, YJKPiping<Pipe> hyMain)
		{
			YJKLine hyline = new YJKLine(hySide.StartConnector.Origin, hySide.EndConnector.Origin);
			YJKLine hyline2 = new YJKLine(hyMain.StartConnector.Origin, hyMain.EndConnector.Origin);
			hyline2.MakeUnBound();
			if (hyline2.GetDistance(hyline.EndPoint) <= hyline2.GetDistance(hyline.StartPoint))
			{
				return hyline.EndPoint;
			}
			return hyline.StartPoint;
		}

		private List<Pipe> m_pipeList;
	}
}
